﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Data;

namespace Hangout.Client.Controls.Hangouts
{
    public partial class HangoutList : UserControl, INotifyPropertyChanged
    {


        public delegate void MoreHangoutClickedHelper(int take, int skip);


        public delegate void ProfilePicSelectedHelper(MeetupService.Meetup data);

        public delegate void StatusTextSelectedHelper(MeetupService.Meetup data);

        public delegate void ProfileNameSelectedHelper(MeetupService.Meetup data);

        public delegate void PinSelectedHelper(MeetupService.Meetup data);

        public delegate void UnPinSelectedHelper(MeetupService.Meetup data);

        public event ProfilePicSelectedHelper ProfilePicSeletcted;

        public event ProfileNameSelectedHelper ProfileNameSeletcted;

        public event StatusTextSelectedHelper StatusTextSeletcted;

        public event PinSelectedHelper PinSeletcted;

        public event UnPinSelectedHelper UnpinSeletcted;

        public event EventHandler ShoutNewHangoutSelected;


        public event MoreHangoutClickedHelper MoreHangoutClicked;

        

        public bool showMoreBtn;

        public bool ShowMoreButton
        {

            get
            {
                try
                {
                    return showMoreBtn;
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                    return false;
                }
            }
            set
            {
                try
                {
                    showMoreBtn = value;
                    if (value)
                    {
                        MoreHangoutsBtn.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        MoreHangoutsBtn.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }
            }
        }

        public bool ShowMorePG
        {
            set
            {
                try
                {
                    if (value)
                    {
                        MorePG.Visibility = System.Windows.Visibility.Visible;
                        MoreHangoutsBtn.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        MorePG.Visibility = System.Windows.Visibility.Collapsed;
                        if (ShowMoreButton)
                        {

                            MoreHangoutsBtn.Visibility = System.Windows.Visibility.Visible;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }

            }
        }
        

        public int Skip { get; set; }

        public int Take = 10; 

        public delegate void HangoutSelectedHelper(MeetupService.Meetup e);

        public event HangoutSelectedHelper HangoutSelected;


        private List<MeetupService.Meetup> hangouts=new List<MeetupService.Meetup>();

        public List<MeetupService.Meetup> Hangouts
        {
            get
            {
                try
                {
                    if (hangouts == null)
                    {
                        hangouts = new List<MeetupService.Meetup>();
                    }
                    return hangouts;
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                    return new List<MeetupService.Meetup>();
                }

            }
            set
            {
                try
                {
                    hangouts = value;

                   

                    FirePropertyChanged("Hangouts");
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }
                
            }
        }

        public HangoutList()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Skip = 0;
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion


        protected void FirePropertyChanged(string propertyName)
        {
            try
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void HangoutLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (HangoutLB.SelectedItem != null)
                {
                    if (HangoutSelected != null)
                    {
                        this.HangoutSelected((MeetupService.Meetup)HangoutLB.SelectedItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

       

       

        private void ProfilePicImage_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (HangoutLB.SelectedItem != null)
                {
                    if (ProfilePicSeletcted != null)
                    {
                        this.ProfilePicSeletcted((MeetupService.Meetup)HangoutLB.SelectedItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }

        private void ShoutText_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (HangoutLB.SelectedItem != null)
                {
                    if (StatusTextSeletcted != null)
                    {
                        this.StatusTextSeletcted((MeetupService.Meetup)HangoutLB.SelectedItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void PinImage_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (HangoutLB.SelectedItem != null)
                {
                    MeetupService.Meetup data = (MeetupService.Meetup)HangoutLB.SelectedItem;
                    if (data.IsPinned)
                    {
                        //raise unpin hangler
                        UnpinSeletcted(data);
                    }
                    else
                    {
                        PinSeletcted(data);
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void ProfileName_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (HangoutLB.SelectedItem != null)
                {
                    if (ProfileNameSeletcted != null)
                    {
                        this.ProfileNameSeletcted((MeetupService.Meetup)HangoutLB.SelectedItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }


        public void RemoveHangoutData(MeetupService.Meetup data)
        {
            try
            {
                if (Hangouts.Contains(data))
                {
                    Hangouts.Remove(data);
                    RefreshList();
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        public void DisplayPinImage(MeetupService.Meetup data)
        {
            try
            {
                if (Hangouts.Contains(data))
                {
                    Hangouts.Remove(data);
                    data.IsPinned = false;
                    Hangouts.Add(data);
                    RefreshList();
                    FirePropertyChanged("Hangouts");
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        public void DisplayUnPinImage(MeetupService.Meetup data)
        {
            try
            {
                if (Hangouts.Contains(data))
                {
                    Hangouts.Remove(data);
                    data.IsPinned = true;
                    Hangouts.Add(data);
                    RefreshList();
                    FirePropertyChanged("Hangouts");
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        public void RefreshList()
        {
            try
            {
                if (hangouts.Count > 0)
                {
                    Hangouts = Hangouts.OrderByDescending(o => o.DateTimePosted).ToList();
                    HangoutLB.ItemsSource = null;
                    HangoutLB.DataContext = Hangouts;
                    HangoutLB.Visibility = System.Windows.Visibility.Visible;
                    HangoutLB.ItemsSource = Hangouts;
                    NoLBL.Visibility = System.Windows.Visibility.Collapsed;

                }
                else
                {
                    HangoutLB.ItemsSource = null;
                    NoLBL.Visibility = System.Windows.Visibility.Visible;
                    HangoutLB.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }

        private void MoreHangoutsBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (showMoreBtn)
                {
                    if (MoreHangoutClicked != null)
                    {
                        Skip += Take;
                        MoreHangoutClicked(Take, Skip);
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void NoLBL_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (ShoutNewHangoutSelected != null)
                {
                    ShoutNewHangoutSelected(null, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }




        internal void RefreshList(bool IsStart)
        {
            try
            {

                if (IsStart)
                {
                    if (hangouts.Count > 0)
                    {
                        Hangouts = Hangouts.OrderByDescending(o => o.DateTimePosted).ToList();
                        HangoutLB.ItemsSource = null;
                        HangoutLB.DataContext = Hangouts;
                        HangoutLB.Visibility = System.Windows.Visibility.Visible;
                        HangoutLB.ItemsSource = Hangouts;
                    }
                    else
                    {
                        HangoutLB.ItemsSource = null;
                        HangoutLB.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
                else
                {
                    RefreshList();
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }


        }
    }
}
